#include "loginandregistration.h"
#include "ui_loginandregistration.h"
#include <registeraccount.h>
#include "forgetpassword.h"

LoginAndRegistration::LoginAndRegistration(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginAndRegistration)
{
    ui->setupUi(this);
    LoginLayout();//登录布局函数
    //实例化自定义数据库
    dataBase = new MySqlite;
}

void LoginAndRegistration::LoginLayout()
{
    //去窗口边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    //把窗口背景设置为透明;
    setAttribute(Qt::WA_TranslucentBackground);
}

void LoginAndRegistration::mouseMoveEvent(QMouseEvent *e)
{
    if (e->buttons() & Qt::LeftButton) {
               this->move(this->x() + e->globalX() - p.x(), this->y() + e->globalY() - p.y());
               p = e->globalPos();
           }
}

void LoginAndRegistration::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
               p = e->globalPos();
    }
}

void LoginAndRegistration::InitDataBaseHeader()
{
    //定义一个查询
    QSqlQuery query;
    //定义一个执行语句
    QString sql;
    //创建一个数据库标题(表头) 并执行  \连接符
    sql="CREATE TABLE user(\
            name varchar(64),\
            password varchar(64)\
            );" ;
    dataBase->excutSQL(query,sql);
}

void LoginAndRegistration::AddDataBaseData()
{
    //定义一个查询
    QSqlQuery query;
    //定义一个执行语句
    QString sql;
    sql = "INSERT INTO user(name,password)\
            VALUES (ui->lineEdit->text(),ui->lineEdit_2->text());";
    dataBase->excutSQL(query,sql);
}

void LoginAndRegistration::DeleteDataBaseData()
{
    QSqlQuery query; //定义一个查询
    QString sql;    //定义一个执行语句
    sql = "DELETE FROM user\
            WHERE name='刘世春';";
    dataBase->excutSQL(query,sql);
}

void LoginAndRegistration::UPDataBaseData()
{
    QSqlQuery query; //定义一个查询
    QString sql;    //定义一个执行语句
    sql = "UPDATE user SET password = '刘世春' WHERE name = '李宁';";
    dataBase->excutSQL(query,sql);
}

void LoginAndRegistration::SelectDataBaseData()
{
    QString sql = "SELECT * FROM user;";
    QSqlQuery query; //定义一个查询
    dataBase->excutSQL(query,sql);
    //遍历查询到的所有的记录
    while (query.next()==true) {
    QString name = query.value(0).toString();
    QString password = query.value(1).toString();
    pUserMap.insert(name,password);//将查询到信息存入映射
//    qDebug()<<name<<" "<<password<<" "<<endl;
    }
    QMap<QString, QString>::iterator iter;
//    for (iter = pUserMap.begin(); iter != pUserMap.end(); ++iter) {
//        qDebug() << "Key:" << iter.key() << ", Value:" << iter.value();
//    }
}


LoginAndRegistration::~LoginAndRegistration()
{
    delete ui;
    delete dataBase;
}

void LoginAndRegistration::on_toolButton_clicked()
{
    showMinimized();
}

void LoginAndRegistration::on_toolButton_2_clicked()
{
     close();
}

void LoginAndRegistration::on_pushButton_clicked()
{
    //获取当前用户输入的内容
    QString user_current=ui->lineEdit->text();
    QString password_current=ui->lineEdit_2->text();
    //遍历映射 查询数据库
    SelectDataBaseData();
    QMap<QString, QString>::iterator iter;
    for (iter = pUserMap.begin(); iter != pUserMap.end(); ++iter) {
        if(iter.key()==user_current&&iter.value()==password_current)
        {
            QMessageBox::about(this,"登录成功","你已成功登录");
            hide();
            terminalPage = new TerminalConsole;
            terminalPage->show();
        }
    }
}

void LoginAndRegistration::on_pushButton_2_clicked()
{
    hide();
    RegisterAccount * registerPage = new RegisterAccount;
    registerPage->show();
}

void LoginAndRegistration::on_pushButton_3_clicked()
{
    hide();
    ForgetPassword * forgetPass = new ForgetPassword;
    forgetPass->show();
}
